import 'package:flutter_module/common/widgets/CommonWidget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';


class RefreshDemoPage extends StatefulWidget {
  const RefreshDemoPage({Key? key}) : super(key: key);

  @override
  State<RefreshDemoPage> createState() => _RefreshDemoPageState();
}

class _RefreshDemoPageState extends State<RefreshDemoPage> {
  List<String> items = ["1", "2", "3", "4", "5", "6", "7", "8"];
  final RefreshController _refreshController = RefreshController();

  void _onRefresh() async {
    // monitor network fetch
    await Future.delayed(const Duration(milliseconds: 1000));
    // if failed,use refreshFailed()
    _refreshController.refreshCompleted();
  }

  void _onLoading() async {
    await Future.delayed(const Duration(milliseconds: 1000));
    items.add((items.length + 1).toString());
    if (mounted) setState(() {});
    _refreshController.loadComplete();
  }

  @override
  Widget build(BuildContext context) {
    return YouthToolBar(
        title: "上拉下拉测试页面",
        onPressed: () => BoostNavigator.instance.pop(),
        body: youthRefresher(
            refreshController: _refreshController,
            child: ListView.builder(
              itemBuilder: (c, i) => Card(child: Center(child: Text(items[i]))),
              itemExtent: 100.0,
              itemCount: items.length,
            ),
            onRefresh: _onRefresh,
            onLoading: _onLoading));
  }
}
